package org.zjt.platform.springbootreactivedemo.config.servlet;

import com.alibaba.druid.support.json.JSONUtils;
import lombok.extern.slf4j.Slf4j;

import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

//import javax.servlet.*;
import java.io.IOException;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Collections;

/**
 * @author juntao.zhang
 * @version V1.0
 * @Description: 配置 servlet 容器
 * @Package org.zjt.platform.springbootreactivedemo.config.servlet
 * @date 2018/6/12 23:48
 *
 * {@link TomcatServletWebServerFactory }
 * {@link JettyServletWebServerFactory}
 * {@link UndertowServletWebServerFactory}
 */
//@Configuration
@Slf4j
public class WebServerFactoryCustomizationBean  implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    private final ServerProperties serverProperties;

    public WebServerFactoryCustomizationBean(ServerProperties serverProperties) {
        log.info("服务器配置信息:{}",JSONUtils.toJSONString(serverProperties));
        this.serverProperties = serverProperties;
    }

    @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        factory.setPort(9000);
    }

    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.setPort(9000);
        Session session = new Session();
        session.setTimeout(Duration.of(10, ChronoUnit.SECONDS));
        factory.setSession(session);
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
        return factory;
    }


/*

import org.apache.catalina.Host;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;

    @Bean
    public TomcatServletWebServerFactory servletWebServerFactory() {
        return new TomcatServletWebServerFactory() {

            @Override
            protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
                super.prepareContext(host, initializers);
                StandardContext child = new StandardContext();
                child.addLifecycleListener(new Tomcat.FixContextListener());
                child.setPath("/cloudfoundryapplication");
                ServletContainerInitializer initializer = getServletContextInitializer(getContextPath());
                child.addServletContainerInitializer(initializer, Collections.emptySet());
                child.setCrossContext(true);
                host.addChild(child);
            }

        };
    }


    *//**
     * 得到一个 servlet 注册器
     * @param contextPath
     * @return
     *//*
    private ServletContainerInitializer getServletContextInitializer(String contextPath) {

        return (c, context) -> {

            // 声明一个servlet。
            Servlet servlet = new GenericServlet() {

                @Override
                public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {

                    ServletContext context = req.getServletContext().getContext(contextPath);
                    context.getRequestDispatcher("/cloudfoundryapplication").forward(req, res);

                }

            };
            // 注册 servlet
            context.addServlet("cloudfoundry", servlet).addMapping("/*");
        };
    }*/
}
